import asyncio


async def waiter_task(name: str, sem):
    count: int = 0
    while True:
        print(f"{name} waiting...")
        # 等待信号量
        async with sem:
            print(f"{name} ok,,, {count}")
            count += 1
            await asyncio.sleep(0.2)


async def cmd_control(sem):
    help = """输入命令:
1: sem.release +1
2: sem.release +2
3: sem.acquire -1
"""
    while True:
        cmd = input(help)
        if cmd == "1":
            sem.release()
        elif cmd == "2":
            sem.release()
            sem.release()
        elif cmd == "3":
            if sem.locked():    # 判断是否上锁
                sem.release()
            sem.acquire()
        else:
            print("未知命令")
        await asyncio.sleep(1)


async def main():
    sem = asyncio.Semaphore(0)
    await asyncio.gather(
        waiter_task("taskA", sem),
        waiter_task("taskB", sem),
        cmd_control(sem),
    )


if __name__ == "__main__":
    asyncio.run(main())
